package top.hmtools.controllers;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import top.hmtools.base.StringTools;
import top.hmtools.beans.DBConnectInfoBean;
import top.hmtools.commons.AlertUtil;
import top.hmtools.enums.EDBType;
import top.hmtools.model.ResultModel;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.scene.control.TextArea;

import javafx.scene.control.ChoiceBox;

public class AddNewDBConnectionController extends BaseController{
	@FXML
	private Button btn_testConnection;
	@FXML
	private Button btn_reset;
	@FXML
	private Button btn_save;
	@FXML
	private TextField connectionName;
	@FXML
	private ChoiceBox<String> dbType;
	@FXML
	private TextField dbUrl;
	@FXML
	private TextField dbUsername;
	@FXML
	private TextField dbPassword;
	@FXML
	private TextArea dbComment;

	/**
	 * 数据库链接信息id
	 */
	private String dbConnId;

	/**
	 * 测试链接
	 * @param event
	 */
	@FXML
	public void doTestConnection(ActionEvent event) {
		DBConnectInfoBean bean = this.getBean();
		if(bean == null){
			return;
		}
		boolean result = this.dbManageService.testConnection(bean);
		if(result){
			AlertUtil.showInfoAlert("连接成功");
		}else{
			AlertUtil.showWarnAlert("连接失败");
		}
	}

	/**
	 * 重置表单
	 * @param event
	 */
	@FXML
	public void doReset(ActionEvent event) {
		this.connectionName.setText("");
//		this.dbType.setValue(null);
		this.dbUrl.setText("");
		this.dbUsername.setText("");
		this.dbPassword.setText("");

		this.dbComment.setText("");
		AlertUtil.showInfoAlert("重置成功");
	}

	/**
	 * 保存数据库连接信息
	 * @param event
	 */
	@FXML
	public void doSave(ActionEvent event) {
		DBConnectInfoBean bean = this.getBean();
		if(bean==null){
			return;
		}
		
		if(StringTools.isBlank(this.dbConnId)){
			boolean result = this.dbManageService.addOne(bean);
			if(result){
				AlertUtil.showInfoAlert("添加成功");
			}else{
				AlertUtil.showWarnAlert("添加失败");
			}
		}else{
			ResultModel<DBConnectInfoBean> result = this.dbManageService.updateOne(bean);
			if(result.isStatusSuccess()){
				AlertUtil.showInfoAlert(result.getMessage());
			}else{
				AlertUtil.showWarnAlert(result.getMessage());
			}
		}
	}
	
	/**
	 * 将页面的参数组装成bean
	 * @return
	 */
	private DBConnectInfoBean getBean(){
		String name = this.connectionName.getText();
		String dbType = this.dbType.getValue()==null?null:String.valueOf(this.dbType.getValue());
		String url = this.dbUrl.getText();
		String username = this.dbUsername.getText();
		String pwd = this.dbPassword.getText();
		
		String comment = this.dbComment.getText();
		
		if(StringTools.isAnyBlank(name,dbType,url)){
			AlertUtil.showWarnAlert("参数不能为空");
			return null;
		}
		if(!url.toUpperCase().contains("SQLITE") && StringTools.isAnyBlank(username,pwd)){
			AlertUtil.showWarnAlert("用户名或密码不能为空");
			return null;
		}
		
		DBConnectInfoBean bean = new DBConnectInfoBean();
		bean.setId(this.dbConnId);
		bean.setName(name);
		bean.setDb_type(dbType);
		bean.setUrl(url);
		bean.setUsername(username);
		bean.setPassword(pwd);
		
		bean.setComment(comment);
		return bean;
	}
	
	/**
	 * 页面展示时初始化
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ObservableList<String> itemList = FXCollections.observableArrayList();
		int index = 0;
		List<EDBType> dbTypes = new ArrayList<>();
		for(EDBType db:EDBType.values()){
			itemList.add(index,db.getName());
			if(index == 0){
			    this.dbType.setValue(db.getName());
			}
			index++;
		}
		this.dbType.setItems(itemList);
		this.dbType.setUserData(dbTypes);
	}

	/**
	 * 设置数据库链接信息值到各控件
	 * @param dbConn
	 */
	public void setDBConnection(DBConnectInfoBean dbConn) {
		this.connectionName.setText(dbConn.getName());
		this.dbType.setValue(EDBType.getByTypeCode(dbConn.getDb_type()).getName());
		this.dbUrl.setText(dbConn.getUrl());
		this.dbUsername.setText(dbConn.getUsername());
		this.dbPassword.setText(dbConn.getPassword());

		this.dbComment.setText(dbConn.getComment());
		this.dbConnId = dbConn.getId();
	}
}
